<?php
/**
 * 《帐号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/MogilefsUtil.class.php');

class File extends MogilefsUtil {
	public $tbl = Constant::tbl_mg_file;

	public $attribute = array(
		'-' => '请选择',
		'Cleartext-Password' => 'Cleartext-Password',
	);
	
	public $tbls = array(
		'-' => '请选择',
		'fy.cw_payment' => 'fy.cw_payment',
		'fy.cw_salary' => 'fy.cw_salary',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'info' => '分类',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('tbl_option', $this->tbls);
	}

	function tab($controller, $request) {
		$data = array(
			'文件管理' => array('cc'=>'mogilefs.php?do=File.find', 'js'=>'1'),
			'好友信息' => array('cc'=>'jabber.php?do=Roster.find', 'js'=>'1'),
			'好友分组' => array('cc'=>'jabber.php?do=RosterGroup.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mogilefs/file/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function orderby_dd($px) {
		if ($px == '1') {
			return "fid DESC";
		}
	
		return $px;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($name_s)) {
			$where .= sprintf(" AND (name LIKE '%s' OR username LIKE '%s')", $name_s . '%', $name_s . '%');
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby('fid');
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'mogilefs/file/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'mogilefs/file/add');
		return true;
	}
	
	function ajax_add($controller, $request) {
	
		$tbl = $request['tbl'];

		if (empty($tbl) || $tbl == '-') {
			return false;
		}
	
		if (!class_exists("MogileFs")) {
			return false;
		}
	
		// $id = $request['id'];
		$oper = $request['oper'];
	
		$this->_files_clean($id);
	
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
	
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			// die($file->getMessage());
			debug($file->getMessage());
		}
		
		// debug(print_r($file, true));
	
		if ($file->isValid()) {
			$filename = $file->getProp("tmp_name");
				
			$name = $file->getProp('name');
			$ext = $file->getProp('ext');
			// $s = sprintf("name = %s, ext = %s", $name, $ext);
			$rnd = rand(100,1000);
			// debug($s);
			
			$id = substr($name, 0, strpos($name, "."));
	
			$mg = newfs('ydt');
	
			$key = sprintf("%s_%s_%s_%s_%s.%s", $tbl, $id, $oper, time(), $rnd, $ext);
				
			$key = strtolower($key);
				
			try {
				$s = sprintf("key = %s, filename = %s\n", $key, $filename);
				debug($s);
					
				$mg->put($filename, $key, 'upload');
				$mg->close();
			} catch(MogileFsException $e) {
				debug($e);
			}
		}
	
		echo "1";
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mogilefs/file/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$dkey1 = $request['dkey1'];
		$dkey2 = $request['dkey2'];
		
		if (!empty($dkey1) && $dkey1 <> $dkey2) {
			
			if (!class_exists("MogileFs")) {
				return false;
			}
			
			$mg = newfs('ydt');
			try {
					
				$mg->rename($dkey1, $dkey2);
				$mg->close();
			} catch(MogileFsException $e) {
				debug($e);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$fid = $request['fid'];
		$row = $this->_view($fid);
		
		$row = $this->_url($row);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mogilefs/file/view');
		return true;
	}
	
	function _url($row) {
		if (!class_exists("MogileFs")) {
			return $ret;
		}
		
		$mg = newfs('ydt');
		
		try {
		
			$key = $row['dkey'];
			
				$paths = $mg->get($key);
				
				$row['paths'] = $paths;
		
				$row['url'] = $paths['path1'];
		
				debug(print_r($paths, true));
		
				if (preg_match("/(jpg|gif|png)$/i", $key)) {
					$row['type'] = 'image';
				}  else {
					$row['type'] = '';
				}
		
		} catch(MogileFsException $e) {
			debug("error");
		}
		
		return $row;
	}
	
	function _view($fid = 0, $row = null) {
		if ($row == null) {
			$db = newdb($this->dsn_r);
			$fmt = "SELECT * FROM %s WHERE fid = '%s'";
			$sql = sprintf($fmt, $this->tbl, $fid);
			$result = $db->query($sql);
			$row = $db->fetch_array($result);
		}
		
		preg_match('/(_)(\d+)(_)(\d+)(_)(\d+)/', $row['dkey'], $a);
		
		$row['emp'] = $a[4];
		$row['createtime'] = date('Y-m-d H:i:s', $a[6]);
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
}
?>